package com.ca.ui.core.view.card;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.ca.ui.core.R;
import com.ca.ui.core.util.ImageUtils;
import com.ca.ui.core.view.bar.MiniLineProcessBar;

public class AccountCard extends ConstraintLayout {

    private String headerUrl;
    private String name;
    private String grade;
    private long gradeCurrentValue;
    private long gradeMaxValue;

    public ImageView headerView;
    public TextView nameView;
    public TextView gradeView;
    public TextView gradeValueView;
    public MiniLineProcessBar gradeBar;
    public ImageView codeIcon;
    public ImageView rightView;

    public AccountCard(@NonNull Context context) {
        super(context);
        initView(context, null);
    }

    public AccountCard(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public AccountCard(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        View view = LayoutInflater.from(context).inflate(R.layout.account_card_layout, this);

        headerView = view.findViewById(R.id.iv_header);
        nameView = view.findViewById(R.id.tv_name);
        gradeView = view.findViewById(R.id.tv_grade);
        gradeValueView = view.findViewById(R.id.tv_gradeValue);
        gradeBar = view.findViewById(R.id.bar_grade);
        codeIcon = view.findViewById(R.id.iv_code);
        rightView = view.findViewById(R.id.iv_right);

        if (attrs != null) {
            TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.AccountCard, 0, 0);

            headerUrl = array.getString(R.styleable.AccountCard_ac_headerUrl);
            name = array.getString(R.styleable.AccountCard_ac_name);
            grade = array.getString(R.styleable.AccountCard_ac_grade);
            gradeCurrentValue = array.getInteger(R.styleable.AccountCard_ac_currentGrade, 0);
            gradeMaxValue = array.getInteger(R.styleable.AccountCard_ac_maxGrade, 0);

            array.recycle();
        }

        setHeaderUrl(headerUrl);
        setName(name);
        setGrade(grade);
        setGradeCurrentValue(gradeCurrentValue);
        setGradeMaxValue(gradeMaxValue);

        //  点击事件
        setOnClickListener(v -> {});
    }

    public String getHeaderUrl() {
        return headerUrl;
    }

    public void setHeaderUrl(String headerUrl) {
        this.headerUrl = headerUrl == null ? "" : headerUrl;
        ImageUtils.loadCircleImage(getContext(), this.headerUrl, headerView);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name == null ? "" : name;
        nameView.setText(this.name);
    }

    public String getGrade() {
        return grade;
    }

    public void setGrade(String grade) {
        this.grade = grade == null ? "1" : grade;
        String text = "Lv." + this.grade + "  ";
        gradeView.setText(text);
    }

    public long getGradeCurrentValue() {
        return gradeCurrentValue;
    }

    public void setGradeCurrentValue(long gradeCurrentValue) {
        this.gradeCurrentValue = gradeCurrentValue;
        String text = this.gradeCurrentValue + "/" + this.gradeMaxValue;
        gradeValueView.setText(text);
        float ratio = 1.0f * this.gradeCurrentValue / this.gradeMaxValue;
        gradeBar.setRatio(ratio);
    }

    public long getGradeMaxValue() {
        return gradeMaxValue;
    }

    public void setGradeMaxValue(long gradeMaxValue) {
        this.gradeMaxValue = gradeMaxValue;
        String text = this.gradeCurrentValue + "/" + this.gradeMaxValue;
        gradeValueView.setText(text);
        float ratio = 1.0f * this.gradeCurrentValue / this.gradeMaxValue;
        gradeBar.setRatio(ratio);
    }
}
